var referId = T.p("referId");
var referType = T.p("referType");
$(function () {
    $("#jqGrid").jqGrid({
        url: '../scheduleInstance/list',
        datatype: "json",
        postData:{referId:referId},
        colModel: [
            {label: 'id', name: 'id', width: 50, key: true,hidden:true},
            {label: '实例Id', name: 'instanceId', width: 90},
            {label: '关联类型', name: 'referType', width: 40, formatter: function (value, options, row) {
                    return T.showDictItemName('inst_refer_type',value);
                }},
            {label: '关联Id', name: 'referId', width: 30},
            {label: '关联名称', name: 'referName', width: 60},
            {label: '运行状态', name: 'status', width: 60, formatter: function (value, options, row) {
                    var rt;
                    if (value == 1) {
                        rt = '<span class="label label-default">待执行</span>';
                    }else if (value == 2) {
                        rt = '<span class="label label-primary">再执行</span>';
                    }else if (value == 3) {
                        rt = '<span class="label label-info">执行中</span>';
                    } else if (value == 4) {
                        rt = '<span class="label label-success">执行完成</span>';
                    }
                    return rt;}
            },
            {label: '创建IP', name: 'createIp', width: 70},
            {label: '执行IP', name: 'execIp', width: 70},
            {label: 'remark', name: 'remark', width: 100},
            {label: 'createTime', name: 'createTime', width: 80},
            {label: 'updateTime', name: 'updateTime', width: 80}
        ],
		viewrecords: true,
        height: 800,
        rowNum: 20,
		rowList : [20,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
			root: "page.data",
			page: "page.pageNo",
			total: "page.pageCount",
			records: "page.count"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        },
        ondblClickRow: function(id){
            var rowData = $("#jqGrid").jqGrid('getRowData',id);
            location.href = "/skd/scheduleInstanceDtl.html?instanceId=" + rowData.instanceId;
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		isShowCond:false,
        btnText:'查询',
        qryForm:{
        }
	},
	methods: {
		reset: function(){

        },
        query: function () {
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{
                },
                page:1
            }).trigger("reloadGrid");
        },
        skdLog: function(){
            var id = getSelectedRow();
            if(id == null){
                return ;
            }

            var rowData = $("#jqGrid").jqGrid('getRowData',id);
            location.href = "/skd/scheduleLog.html?instanceId=" + rowData.instanceId;

        },
        dtlMgr: function () {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }

            var rowData = $("#jqGrid").jqGrid('getRowData',id);
            location.href = "/skd/scheduleInstanceDtl.html?instanceId=" + rowData.instanceId;
        },
        runOnce: function () {
            var jobIds = getSelectedRows();
            if(jobIds == null){
                return ;
            }
            confirm('确定要立即执行选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: "/scheduleInstance/run",
                    data: JSON.stringify(jobIds),
                    success: function(r){
                        console.log(r);
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },completedInst: function () {
            var jobIds = getSelectedRows();
            if(jobIds == null){
                return ;
            }
            confirm('确定要立即执行选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: "/scheduleInstance/completedInst",
                    data: JSON.stringify(jobIds),
                    success: function(r){
                        console.log(r);
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        toggle:function(){
            this.isShowCond = !this.isShowCond;

            if(this.isShowCond==true){
                this.btnText = "隐藏"
            }else if(this.isShowCond==false){
                this.btnText = "查询"
            }

        },
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			
			location.href = "scheduleInstance_add.html?id="+id;
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../scheduleInstance/delete",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		}
	}
});

setInterval(function(){
    $("#jqGrid").trigger("reloadGrid");
},10*1000);